<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen
    @ok="handleOk" @cancel="handleCancel" cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">


        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="炼厂名称">
          <a-input placeholder="请输入炼厂名称" :disabled='true' v-model="refineryName" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="选择油品">
          <a-checkbox-group v-model="selectList">
            <a-row>
              <a-col :span="8" v-for="item in typelist">
                <a-checkbox @change="onChange" :value="item.id">
                  {{item.name}}
                </a-checkbox>
              </a-col>
            </a-row>
          </a-checkbox-group>
          <div style="color: #ff0000;">该油品为该炼厂内分配的油品，取消勾选，历史记录将被删除，操作不可逆！</div>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    httpAction,
    postAction,
    getAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    components: {
      JEditor
    },
    props: {
      type: {
        type: Number,
        default: 1
      }
    },
    name: "YsyRefineryModal",
    data() {
      return {
        selectList: [],
        value2: [],
        title: "分配油品",
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          bandOitTypeList: '/app/ysyOilType/bandOitTypeList',
          oilTypeList: '/app/ysyOilType/list',
          getOilTypeByRefineryId: '/app/ysyOilType/getOilTypeByRefineryId/',
          cancelBandOitTypeList: '/app/ysyOilType/cancelBandOitTypeList'
        },
        refineryId: '',
        list: [],
        oilRawMaterialIds: [],
        typelist: [],
        ysyRefineryId: '',
        refineryName: ''
      }
    },
    created() {},
    methods: {
      onChange(e) {
        if (!e.target.checked) {
          //取消
          let params = {
            ysyRefineryId: this.ysyRefineryId,
            ysyOilTypeId: e.target.value
          }
          postAction(this.url.cancelBandOitTypeList, params).then((res) => {
            this.$message.success('已解绑');
          })
        } else {
          //绑定
          let params = {
            ysyRefineryId: this.ysyRefineryId,
            ysyOilTypeId: e.target.value
          }
          postAction(this.url.bandOitTypeList, params).then((res) => {
            this.$message.success('绑定成功');
          })
        }
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.ysyRefineryId = record.refineryId
        this.getOilTypeList()
        this.getOilTypeByRefineryId()
        this.form.resetFields();
        this.model = Object.assign({}, record);
        if (record.refineryId) {
          this.refineryId = record.refineryId
        }
        this.refineryName = record.refineryName

      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.refineryId = ''
        this.value2 = []
        this.selectList = []
      },
      getOilTypeByRefineryId() {
        getAction(this.url.getOilTypeByRefineryId + this.ysyRefineryId).then((res) => {
          this.selectList = res.result.map(item => item.id)
          this.visible = true;
        })
      },
      getOilTypeList() {
        let params = {
          pageNo: 1,
          pageSize: 9999
        }
        getAction(this.url.oilTypeList, params).then((res) => {
          this.typelist = res.result.records
        })
      },
      handleOk() {
        this.visible = false;
      },
      handleCancel() {
        this.close()
      },

    }
  }
</script>

<style lang="less" scoped>

</style>